package hhs.poc.activities.whoiswho;

import hhs.poc.activities.R;
import hhs.poc.util.utils;

import java.io.File;
import java.util.ArrayList;

import com.googlecode.tesseract.android.R.array;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

public class Main extends Activity {
	private Button shoot_button;
	private EditText room_input;
	private Spinner building_input;
	private String room_nr;
	private String building_name;
	private String[] room;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			initComponents();

			// Data from photo
			Bundle bundle = getIntent().getExtras();
			String ocrResult = bundle.getString("ocrResult");

			room = ocrResult.split(" ");
			building_name = room[0];
			room_nr = room[1];
			String[] building_array = getResources().getStringArray(
					R.array.building_array);

			building_input = (Spinner) findViewById(R.id.building_input);
			int position = 0;
			for (String ba : building_array) {
				if (ba.equals(building_name)) {
					building_input.setSelection(position);
				}
				position += 1;
			}

			room_input = (EditText) findViewById(R.id.roomnr_input);
			room_input.setText(room_nr);

			shoot_button = (Button) findViewById(R.id.shoot_button);
			setButtonListener(shoot_button, Main.this, Menu.class);
		} catch (Exception e) {
			Log.e("ERROR", e.toString());
			e.printStackTrace();
		}
	}

	private void initComponents() {
		utils.setTitleBackgroundColor(findViewById(android.R.id.title));
		setContentView(R.layout.main);
	}

	public void setButtonListener(Button button, final Activity current,
			final Class<?> target) {
		button.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(current, target);
				building_name = building_input.getSelectedItem().toString();
				room_nr = room_input.getText().toString();
				if (!room_nr.contains(".")){
					String[] chars = room_nr.split("");
					room_nr = "";
					for (int j = 0; j < chars.length; j++){
						if (j == 2){
							room_nr = room_nr + "." + chars[j];
						} else {
							room_nr = room_nr + chars[j];
						}
					}
				}
				String br = building_name + " " + room_nr;
				i.putExtra("room_nr", br);
				startActivity(i);
			}
		});
	}
}